<?php

namespace IMT\DashboardBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class CalendarController extends Controller
{
    
    public function allEventAction()
    {
    	$user = $this->container->get('security.context')->getToken()->getUser();
    	$accounts = $this->getDoctrine()->getRepository('IMTAccountBundle:Account')->findAllByUser($user);
    	$return = array();
    	
    	foreach($accounts as $account){
    		foreach($account->getEvents() as $eventLink){
    			$event = $eventLink->getEvent();
    			$participants = $event->getParticipants();
    			$owner = $participants[0]->getParticipant()->getPerson();
	    		$return[] = array(
	    			"id" => $event->getId(),
	    			"owner_name" => $owner->getName(),
	    			"owner_firstname" => $owner->getFirstname(),
	    			"title" => $event->getTitle(),
	    			"start" => date('r', $event->getDate()->getTimestamp()),
	    			"end" => date('r', $event->getDateFin()->getTimestamp()),
    				"start_f" => date('\L\e d/m/Y H:i\h', $event->getDate()->getTimestamp()),
    				"end_f" => date('\L\e d/m/Y H:i\h', $event->getDateFin()->getTimestamp()),
	    			"allDay" => ($event->getAllDay() == 0) ? false : true,
	    			"content" => $event->getContent(),
	    			"account" => $account->getId(),
	    			"className" => $account->getColor()
	    		);
    		}
    	}
    	
        $return = json_encode($return);
	   	return new Response($return,200);
    }
    
    public function updateEventAction($type)
    {
    	$return = array('response'=>500, 'params'=>'Une erreur inconnue est survenue');
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();
    	
    	if('POST' == $request->getMethod()){
    		$idEvent = $request->request->get('idEvent');
    		$event = $this->getDoctrine()->getRepository('IMTEventBundle:Event')->find($idEvent);
    		
    		switch($type)
    		{
    			case 'move':
    				$dayDelta = $request->request->get('dayDelta');
    				$minuteDelta = $request->request->get('minuteDelta');
    				$allDay = (intval($request->request->get('allDay')) == 0) ? false : true;
    				
    				$newDate = date('Y-m-d H:i:s', $event->getDate()->getTimestamp() + (intval($dayDelta) * 24 * 3600) + (intval($minuteDelta) * 60));
    				$newDateFin = date('Y-m-d H:i:s', $event->getDateFin()->getTimestamp() + (intval($dayDelta) * 24 * 3600) + (intval($minuteDelta) * 60));
    				
    				$event->setDate(new \DateTime($newDate));
    				$event->setDateFin(new \DateTime($newDateFin));
    				$event->setAllDay($allDay);
    				
    				try {
	    				$em->persist($event);
	    				$em->flush();
	    				
	    				$return = array('response'=>200);
    				} catch(\Exception $e){
    					return new Response(json_encode(array('response'=>500, 'params'=>'Une erreur est survenue au moment de l\'enregistrement de l\'évenement', 'debug' => $e->getMessage())),200);
    				}
    				break;
    			case 'resize':
    				$dayDelta = $request->request->get('dayDelta');
    				$minuteDelta = $request->request->get('minuteDelta');
    				
    				$newDateFin = date('Y-m-d H:i:s', $event->getDateFin()->getTimestamp() + (intval($dayDelta) * 24 * 3600) + (intval($minuteDelta) * 60));
    				
    				$event->setDateFin(new \DateTime($newDateFin));
    				
    				try {
    					$em->persist($event);
    					$em->flush();
    					 
    					$return = array('response'=>200);
    				} catch(\Exception $e){
    					return new Response(json_encode(array('response'=>500, 'params'=>'Une erreur est survenue au moment de l\'enregistrement de l\'évenement', 'debug' => $e->getMessage())),200);
    				}
    				break;
    			default:
    				return new Response(json_encode(array('response'=>500, 'params'=>'Le paramètre type reçus est incorrect ('.$type.')')),200);
    		}
    		
    	} else
    		return new Response(json_encode(array('response'=>500, 'params'=>'Les paramètres reçus sont incorrects')),200);
    	
    	$return = json_encode($return);
    	return new Response($return,200);
    }
}
